﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Web.Controllers
{
    public class HistoryController : CustomBaseController
    {

        private HistoryModel InitalModel(HistoryModel model)
        {
            WebConfigProvider wcp = new WebConfigProvider();
            IUserService us = new UserService(wcp);
            Data.User user = us.GetUserItemByCIL(User.Identity.Name);
            if (user != null)
                model.UserId = user.UserID;
            else
                model.UserId = 0;
            if (model.page == 0)
            {
                model.page = 1;
            }
            if (model.key == null)
            {
                model.key = "";
            }
            if (model.category == null || model.category != "company")
                model.category = "self";
            model.IsChangeUrl = false;

            return model;
        }
        //
        // GET: /History/

        [HttpPost]
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult Resentmessage(HistoryModel model)//int Id, int Batchid, string category,int page)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            dataAccess.ResentMessageById(model.Id);
            model = InitalModel(model);
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);

            if (model.category.ToLower() == "self")
            {
                model.Infos = dataAccess.GetHistoryByUser(model.page, maxshow, model.UserId, ViewHistoryType.failed, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.User, model.UserId, maxshow, model.key, model.BatchId);
            }
            else
            {
                model.Infos = dataAccess.GetHistoryByAll(model.page, maxshow, ViewHistoryType.failed, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.All, 0, maxshow, model.key, model.BatchId);
            }
            return View("historygrid_failed", model);
        }



        [HttpPost]
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult ResentOrDeleteMessages(string[] item, HistoryModel model, string todo)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);

            model = InitalModel(model);

            if (item != null && item.Length != 0)
            {
                todo = todo.Trim();
                todo = todo.ToLower();
                if (todo == "重发" || todo == "resend")
                {
                    foreach (var id in item)
                        dataAccess.ResentMessageById(Convert.ToInt32(id));
                }
                else
                {
                    foreach (var id in item)
                        dataAccess.DeleteMessageById(Convert.ToInt32(id));
                }
            }
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);

            if (model.category.ToLower() == "self")
            {
                model.Infos = dataAccess.GetHistoryByUser(model.page, maxshow, model.UserId, ViewHistoryType.failed, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.User, model.UserId, maxshow, model.key, model.BatchId);
            }
            else
            {
                model.Infos = dataAccess.GetHistoryByAll(model.page, maxshow, ViewHistoryType.failed, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.All, 0, maxshow, model.key, model.BatchId);
            }

            return View("historygrid_failed", model);
        }

        [HttpPost]
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult ForwardOrDeleteMessages(string[] item, string todo, HistoryModel model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);

            model = InitalModel(model);

            if (item != null && item.Length != 0)
            {
                todo = todo.Trim();
                todo = todo.ToLower();
                if (todo == "删除" || todo == "delete")
                {
                    //    foreach (var id in item)
                    //        dataAccess.ResentMessageById(Convert.ToInt32(id));
                    //}
                    //else
                    //{
                    foreach (var id in item)
                        dataAccess.DeleteMessageById(Convert.ToInt32(id));
                }
            }
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);

            if (model.category.ToLower() == "self")
            {
                model.Infos = dataAccess.GetHistoryByUser(model.page, maxshow, model.UserId, ViewHistoryType.delivered, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.User, model.UserId, maxshow, model.key, model.BatchId);
            }
            else
            {
                model.Infos = dataAccess.GetHistoryByAll(model.page, maxshow, ViewHistoryType.delivered, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.All, 0, maxshow, model.key, model.BatchId);
            }

            return View("historygrid_delivered", model);
        }

        [HttpPost]
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult Deletemessage_Delivered(HistoryModel model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            dataAccess.DeleteMessageById(model.Id);
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            model = InitalModel(model);
            if (model.category.ToLower() == "self")
            {
                model.Infos = dataAccess.GetHistoryByUser(model.page, maxshow, model.UserId, ViewHistoryType.delivered, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.User, model.UserId, maxshow, model.key, model.BatchId);
            }
            else
            {
                model.Infos = dataAccess.GetHistoryByAll(model.page, maxshow, ViewHistoryType.delivered, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.All, 0, maxshow, model.key, model.BatchId);
            }
            return View("historygrid_delivered", model);
        }

        /// <summary>
        /// 失败页面的删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult Deletemessage(HistoryModel model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            dataAccess.DeleteMessageById(model.Id);
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            model = InitalModel(model);
            if (model.category.ToLower() == "self")
            {
                model.Infos = dataAccess.GetHistoryByUser(model.page, maxshow, model.UserId, ViewHistoryType.failed, model.key,model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.User, model.UserId, maxshow, model.key, model.BatchId);
            }
            else
            {
                model.Infos = dataAccess.GetHistoryByAll(model.page, maxshow, ViewHistoryType.failed, model.key, model.BatchId);
                model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.All, 0, maxshow, model.key, model.BatchId);
            }
            return View("historygrid_failed", model);
        }

        public ActionResult Index()
        {
            return View();
        }

        #region Failed
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult Failed(HistoryModel Model)
        {
            if (Model.page == 0)
            {
                Model.page = 1;
            }

            if (Model.category == null)
                Model.category = "";
            Model.IsChangeUrl = true;
            WebConfigProvider wcp = new WebConfigProvider();
            IUserService us = new UserService(wcp);
            Data.User user = us.GetUserItemByCIL(User.Identity.Name);
            Model.UserId = user.UserID;
            var role = user.Role.FirstOrDefault(r => r.RoleName == "Admin");
            if (role == null)
                Model.IsAdmin = false;
            else
                Model.IsAdmin = true;


            switch (Model.category.ToLower())
            {
                //case "group":
                //    return Failed_Group(Model);
                case "company":
                    return Failed_Company(Model);
                default:
                    {
                        Model.category = "self";
                        return Failed_Self(Model);
                    }
            }
        }


        public ViewResult Failed_Self(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByUser(Model.page, maxshow, Model.UserId, ViewHistoryType.failed, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.User, Model.UserId, maxshow, Model.key, Model.BatchId);
            return View("Failed_Self", Model);
        }

        public ViewResult Failed_Group(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByGroup(Model.page, maxshow, 1, ViewHistoryType.failed, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.Group, 1, maxshow, Model.key, Model.BatchId);
            return View("Failed_Group", Model);
        }

        private ViewResult Failed_Company(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByAll(Model.page, maxshow, ViewHistoryType.failed, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.All, 0, maxshow, Model.key, Model.BatchId);
            return View("Failed_Company", Model);
        }
        #endregion

        #region Delivered
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult Delivered(HistoryModel Model)
        {
            if (Model.page == 0)
            {
                Model.page = 1;
            }

            if (Model.category == null)
                Model.category = "";
            Model.IsChangeUrl = true;

            WebConfigProvider wcp = new WebConfigProvider();
            IUserService us = new UserService(wcp);
            Data.User user = us.GetUserItemByCIL(User.Identity.Name);
            Model.UserId = user.UserID;
            var role = user.Role.FirstOrDefault(r => r.RoleName == "Admin");
            if (role == null)
                Model.IsAdmin = false;
            else
                Model.IsAdmin = true;

            switch (Model.category.ToLower())
            {
                //case "group":
                //    return Delivered_Group(Model);
                case "company":
                    return Delivered_Company(Model);
                default:
                    return Delivered_Self(Model);
            }
        }


        public ViewResult Delivered_Self(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByUser(Model.page, maxshow, Model.UserId, ViewHistoryType.delivered, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.User, Model.UserId, maxshow, Model.key, Model.BatchId);
            return View("Delivered_Self", Model);
        }

        public ViewResult Delivered_Group(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByGroup(Model.page, maxshow, 1, ViewHistoryType.delivered, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.Group, 1, maxshow, Model.key, Model.BatchId);
            return View("Delivered_Group", Model);
        }

        private ViewResult Delivered_Company(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByAll(Model.page, maxshow, ViewHistoryType.delivered, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.All, 0, maxshow, Model.key, Model.BatchId);
            return View("Delivered_Company", Model);
        }
        #endregion


        #region Pending
        [CustomAuthorise(Flag = AuthoriseFlag.page_history)]
        public ViewResult Pending(HistoryModel Model)
        {
            if (Model.page == 0)
            {
                Model.page = 1;
            }

            if (Model.category == null)
                Model.category = "";
            Model.IsChangeUrl = true;

            WebConfigProvider wcp = new WebConfigProvider();
            IUserService us = new UserService(wcp);
            Data.User user = us.GetUserItemByCIL(User.Identity.Name);
            Model.UserId = user.UserID;
            var role = user.Role.FirstOrDefault(r => r.RoleName == "Admin");
            if (role == null)
                Model.IsAdmin = false;
            else
                Model.IsAdmin = true;

            switch (Model.category.ToLower())
            {
                //case "group":
                //    return Pending_Group(Model);
                case "company":
                    return Pending_Company(Model);
                default:
                    return Pending_Self(Model);
            }
        }


        public ViewResult Pending_Self(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByUser(Model.page, maxshow, Model.UserId, ViewHistoryType.pending, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.pending, ViewHistoryRole.User, Model.UserId, maxshow, Model.key, Model.BatchId);
            return View("Pending_Self", Model);
        }

        public ViewResult Pending_Group(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByGroup(Model.page, maxshow, 1, ViewHistoryType.pending, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.pending, ViewHistoryRole.Group, 1, maxshow, Model.key, Model.BatchId);
            return View("Pending_Group", Model);
        }

        private ViewResult Pending_Company(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByAll(Model.page, maxshow, ViewHistoryType.pending, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.pending, ViewHistoryRole.All, 0, maxshow, Model.key, Model.BatchId);
            return View("Pending_Company", Model);
        }

        #endregion
        //public ActionResult ResentByBatchId(HistoryModel Model)
        //{
        //    WebConfigProvider configProvider = new WebConfigProvider();
        //    IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
        //    dataAccess.ResentMessage(Model.BatchId);
        //    return RedirectToAction("failed", "history", new { pageindex = 1 , batchid = Model.BatchId});
        //}

    }
}
